<?php
namespace app\teacherComment\controller;
use app\common\Base;
use app\common\Excel;
use think\Db;

class TeacherCommentReg extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		$where = [];
		$termId = input("termId");
		$commentType = input("类别");
		$teacherName = input("teacherName");
		$page = input("page");
		$rows = input("rows");
		if($termId) $where["学期ID"] = $termId;
		if($commentType) $where["类别"] = $commentType;
		if($teacherName) $where["姓名"] = array("like",$teacherName);
		$result = Db::table('教师考评')->alias('a')
				  ->join('学期名称 b','a.学期ID=b.ID','LEFT')
				  ->join('教师信息 c','a.教师ID=c.ID','LEFT')
				  ->join('学科名称 d','c.任教学科ID=d.ID','LEFT')
				  ->field("a.ID,a.类别,a.项目名称,a.项目内容,a.教师ID,a.获奖级别,a.获奖等第,a.获奖时间,a.立项时间,a.结题时间,a.颁发单位,a.加分,a.备注,a.学期ID,b.学期名称,c.姓名,c.年级号,d.学科名称")
				  ->where($where)
				  ->order("学期ID desc,ID desc")
				  ->page($page, $rows)
		          ->select();
		$count = Db::table('教师考评')->alias('a')
				  ->join('教师信息 c','a.教师ID=c.ID','LEFT')
				  ->where($where)
				  ->count();
		$grid['rows'] = selectResult($result);
        $grid['total'] = $count;
		return $grid;
	}
	public function exportExcel() {
		$where = [];
		$termId = input("termId");
		$commentType = input("类别");
		$teacherName = input("teacherName");
		if($termId) $where["学期ID"] = $termId;
		if($commentType) $where["类别"] = $commentType;
		if($teacherName) $where["姓名"] = array("like",$teacherName);
		$result = Db::table('教师考评')->alias('a')
				  ->join('学期名称 b','a.学期ID=b.ID','LEFT')
				  ->join('教师信息 c','a.教师ID=c.ID','LEFT')
				  ->join('学科名称 d','c.任教学科ID=d.ID','LEFT')
				  ->field("a.ID,a.类别,a.项目名称,a.项目内容,a.教师ID,a.获奖级别,a.获奖等第,a.获奖时间,a.立项时间,a.结题时间,a.颁发单位,a.加分,a.备注,a.学期ID,b.学期名称,c.姓名,c.年级号,d.学科名称")
				  ->where($where)
				  ->order("学期ID desc,ID desc")
		          ->select();
		$header = array('学期名称'=>'学期名称','类别'=>'类别','学科名称'=>'学科名称','教师姓名'=>'姓名','项目名称'=>'项目名称','项目内容'=>'项目内容','获奖级别'=>'获奖级别','获奖等第'=>'获奖等第','获奖时间'=>'获奖时间','立项时间'=>'立项时间','结题时间'=>'结题时间','颁发单位'=>'颁发单位','加分'=>'加分','备注'=>'备注');
		Excel::toExcel(selectResult($result),'教师考评',$header);
		exit;
	}
	public function addUpdate() {
		$param = request()->param();
		try {
			if(array_key_exists("ID", $param)) {
				update('教师考评', $param);
			}else {
				insert('教师考评', $param);
			}
			return '000';
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return '999';
	}

	public function del() {
		$delIds = input("delIds");
		try {
			Db::name('教师考评')->where('ID','in',explode(",", $delIds))->delete();
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

}
